
import * as SMap from '../SimpleMap.es'
import Cesium from 'cesium'
let Layer = null;

class ExModel {
    constructor(Model) {
        this.Model = Model;
        const ViewModel = [];
        const ParamsMapping = {};
        const Value = {};
        for (let i = 0; i < Model.Param.length; i++) {
            const element = Model.Param[i];
            ParamsMapping[element.Key] = JSON.parse(JSON.stringify(element));
            if (element.hasOwnProperty('Default')) {
                Value[element.Key] = element.Default
            } else {
                switch (element.FormType) {
                    case 'String':
                        Value[element.Key] = '';
                        break;
                    case 'Number':
                        Value[element.Key] = 0;
                        break;
                    default:
                        Value[element.Key] = null;
                        break;
                }
            }
        }
        for (let i = 0; i < Model.View.length; i++) {
            const element = Model.View[i];
            const ViewItem = {
                title: element.title,
                children: [],
            }
            for (let d = 0; d < element.children.length; d++) {
                const child = element.children[d];
                ParamsMapping[child] && ViewItem.children.push(ParamsMapping[child]);
            }
            ViewModel.push(ViewItem);
        };
        this.ViewModel = ViewModel;
        this.ParamsMapping = ParamsMapping;
        this.Value = Value;
        this.Events = {};
        for (let i = 0; i < Model.Map.RightMenu.length; i++) {
            const element = Model.Map.RightMenu[i];
            this.Events[element.EventName] = new Function('Model,Value', element.CallBack)
        }
        
        this.ParamsEvent = {};

        for (const key in Model.Map.Wacth) {
            const element = Model.Map.Wacth[key];
            this.ParamsEvent[key] = new Function('Entity,Value,Model', element);
        }

        this.Entity = Layer.add({
            id: 'Cache:' + SMap.Util.uuid(),
            image: {
                size: { width: 32, height: 32 },
                color: '#1898ff',// 图标染色
                url: './plane.png',
                ...Model.Map.options.image
            },
            label: {
                text: 'AAA',
                fontSize: 16,
                color: '#FFFFFF',
                BGColor: '#000000AA',  // 文字背景颜色
                offset: [0, -32],  // 偏移量 单位：像素
                horizontal: 'CENTER', // 横向对齐属性  LEFT  /  RIGHT  / CENTER
                vertical: 'CENTER', // 纵向对齐属性  TOP /  BOTTOM  /  CENTER
                ...Model.Map.options.label
            }
        });
        this.Entity.Model = this;

        var Size = Math.max(Model.Map.options.image.size.width, Model.Map.options.image.size.height)

        this.Entity.Entity.point = {
            show: false, //是否展示
            pixelSize: (Size * 1.1) || 40, //点的大小
            color: Cesium.Color.YELLOW.withAlpha(0), //颜色
            outline: true,
            outlineWidth: 2,
            outlineColor: Cesium.Color.fromCssColorString(Model.Map.options.image.color),
            disableDepthTestDistance: Number.POSITIVE_INFINITY,
        }

        for (const key in this.ParamsEvent) {
            const element = this.ParamsEvent[key];
            element(this.Entity, this.Value[key], this.Value)
        }
    }

    set Select(value) {
        this.Entity.Entity.point.show = !!value
    }
    get Select() {
        return this.Entity.Entity.point.show
    }
}

export function CreateExModel(Model) {
    const _Model = new ExModel(Model);
    return _Model;
}


export function SetLayer(_Layer) {
    Layer = _Layer;
}


export function GetLayer() {
    return Layer
}
